package com.yao2san.simdif.service.impl;

import com.yao2san.simdif.bean.request.DiffReportDownloadReq;
import com.yao2san.simdif.service.DbDifService;
import com.yao2san.simdif.service.DiffReportService;
import com.yao2san.simdif.service.Result;
import com.yao2san.utils.ReportUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

@Service
public class DiffReportServiceImpl implements DiffReportService {
    @Autowired
    private DbDifService dbDifService;

    @Override
    public void download(HttpServletResponse response, DiffReportDownloadReq req) {

        Result dif = dbDifService.dif(req);
        String templateContent;
        try {
            response.reset();
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("utf-8");
            response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("数据库结构差异报告.pdf", "UTF-8"));
            templateContent = ReportUtils.getHtml("report.ftlh", dif);
            ReportUtils.html2Pdf(templateContent, response.getOutputStream());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
